﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.DanTocService;
namespace eTMS.Controllers{
public class DanTocControllerBase : CommonController{
	public DanTocControllerBase()
		: base()
	{	
		this.dao = new DanTocServiceClient();
	}
	~DanTocControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private DanTocEntity oDanToc;	
	public DanTocEntity DanTocObject{
		get{
			return oDanToc;
		}
		set{
			oDanToc=value;
		}
	}
	protected DanTocServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSDanToc(false);			
			return _Table;
		}
	}
	public void LoadAllDSDanToc(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oDanToc==null)return false;
		bool result=true;
		try{
			oDanToc = dao.SaveRet(oDanToc);
		}
		catch{
			result=false;
		}
		if(result){
			_ListDanToc.Add(oDanToc);
		}
		return result;
	}
	public bool Update(){
		if(oDanToc==null)return false;
		return dao.Update(oDanToc);
	}
	public bool Delete(){
		if(oDanToc==null)return false;
		bool retvalue = dao.Delete(oDanToc);
		if(retvalue){			
			_ListDanToc.Remove(oDanToc);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 DanToc_ID){
		bool retvalue = dao.DeleteByID(DanToc_ID);		
		if(retvalue){
			DanTocEntity oDeleted=FindDanToc(DanToc_ID);
			if(oDeleted!=null){
				_ListDanToc.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oDanToc = new DanTocEntity();
	}
	//Check if exists MaDanToc
	public bool CheckExistsMaDanToc(object value){	
		return dao.SelectOneByMaDanToc(value.ToString()).MaDanToc!=null;
    }    
    //Copy data from data row in grid to object
    public DanTocEntity MapDataRowToObject(DataRow row){
		DanTocEntity o = new DanTocEntity();
		 o.MaDanToc=Convert.ToString(row["MaDanToc"]);
		 o.TenDanToc=Convert.ToString(row["TenDanToc"]);
		 o.MoTaDanToc=Convert.ToString(row["MoTaDanToc"]);
		 if(!row.IsNull("DanToc_ID"))
		    o.DanToc_ID=(Int32)(row["DanToc_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(DanTocEntity o,DataRow row){
		row["MaDanToc"]=o.MaDanToc;
		row["TenDanToc"]=o.TenDanToc;
		row["MoTaDanToc"]=o.MoTaDanToc;
		row["DanToc_ID"]=o.DanToc_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.DanTocObject,row);
    }
	public void LoadObject(Int32 DanToc_ID){
		DanTocEntity oLoad=FindDanToc(DanToc_ID);
		if(oLoad==null){
			oDanToc=this.LoadDanToc(DanToc_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oDanToc.IsNullDanToc_ID;
			if(!bNotIsNull){
				this.ListDanToc.Add(oDanToc);
			}
		}
		else{
			oDanToc=oLoad;
		}
	}
	#region CachedList DanToc
	private static List<DanTocEntity> _ListDanToc=new List<DanTocEntity>();
    public List<DanTocEntity>ListDanToc{
		get{
			return _ListDanToc;
		}
	}
	public static List<DanTocEntity>ListDanTocStatic{
		get{
			return _ListDanToc;
		}
	}
	public DanTocEntity FindDanToc(Int32 DanToc_ID){
		DanTocEntity oResult=this.ListDanToc.Find(delegate(DanTocEntity oFind){
			bool result=true; 
			result&=oFind.DanToc_ID.Equals(DanToc_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllDanToc(bool clearCache){
		if(_ListDanToc.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		DanTocServiceClient dao = new DanTocServiceClient();
		_ListDanToc=new List<DanTocEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public DanTocEntity LoadDanToc(Int32 DanToc_ID){			
		DanTocEntity o=dao.SelectOne(DanToc_ID);		
		return o;
	}
	#endregion
}
}
